﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectTool.Models;

namespace ProjectTool.Lib
{
  public class CustomAuthorizeAttribute : AuthorizeAttribute
  {
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
      User user = null;

      using (var db = new ProjectEntities())
      {
        user = db.Users.SingleOrDefault(u => u.username == httpContext.User.Identity.Name);
      }

      if (user != null)
      {
        return true;
      }
      else
      {
        httpContext.Response.Redirect(@"\Home\");
        return false;
      }
    }
  }
}